package com.paul.girl.service.impl;

import com.paul.girl.domain.Girl;
import com.paul.girl.exception.GirlException;
import com.paul.girl.repository.GirlRepository;
import com.paul.girl.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GirlServiceImpl implements GirlService {
    @Autowired
    private GirlRepository repository;

    @Override
    public Girl addGirl(Girl girl) {
        return repository.save(girl);
    }

    @Override
    public void deleteGirl(String id) {
        repository.delete(Integer.valueOf(id));
    }

    @Override
    public void updateGirl(Girl girl, String id) {
        girl.setId(Integer.valueOf(id));
        repository.save(girl);
    }

    @Override
    public Girl findGirl(String id) {
        return repository.findOne(Integer.valueOf(id));
    }

    @Override
    public List<Girl> findGirls() {
        return repository.findAll();
    }

    @Override
    public List<Girl> findByAge(Integer age) {
        return repository.findByAge(age);
    }

    @Override
    public Integer getAge(String id) {
        int age = repository.findOne(Integer.parseInt(id)).getAge();
        if (age <= 10) {
            //code=100
            throw new GirlException("估计还在上小学", 100);
        }
        if (age < 18) {
            //code=200方便排查
            throw new GirlException("估计还在上中学", 200);
        } else
            System.out.println("成年了!!!");
        return age;
    }
}
